<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 22.2</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="22.1.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="23.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P3">Part III. The Standard Libraries</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#22">Chapter 22. The Operating System Library</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>22.2 &ndash; Other System Calls</h2>

<p>The <code>os.exit</code> function terminates the execution of a program.
The <code>os.getenv</code> function gets the value of
an environment variable.
It receives the name of the variable and returns a string with its value:
<pre>
    print(os.getenv("HOME"))    --> /home/lua
</pre>
If the variable is not defined, the call returns <B>nil</B>.
The function <code>os.execute</code> runs a system command;
it is equivalent to the <code>system</code> function in C.
It receives a string with the command
and returns an error code.
For instance, both in Unix and in DOS-Windows,
you can write the following function to create new directories:

<pre>
    function createDir (dirname)
      os.execute("mkdir " .. dirname)
    end
</pre>
The <code>os.execute</code> function is powerful,
but it is also highly system dependent.

<p>The <code>os.setlocale</code> function sets the current
locale used by a Lua program.
Locales define behavior that is sensitive to
cultural or linguistic differences.
The <code>setlocale</code> function has two string parameters:
the locale name and a category,
which specifies what features the locale will affect.
There are six categories of locales:
<code>"collate"</code> controls the alphabetic order of strings;
<code>"ctype"</code> controls the types of individual characters
(e.g., what is a letter)
and the conversion between lower and upper cases;
<code>"monetary"</code> has no influence in Lua programs;
<code>"numeric"</code> controls how numbers are formatted;
<code>"time"</code> controls how date and time are formatted
(i.e., function <code>os.date</code>);
and <code>"all"</code> controls all the above functions.
The default category is <code>"all"</code>,
so that if you call <code>setlocale</code> with only the locale
name it will set all categories.
The <code>setlocale</code> function returns the locale name
or <B>nil</B> if it fails
(usually because the system does not support the given locale).
<pre>
    print(os.setlocale("ISO-8859-1", "collate"))   --> ISO-8859-1
</pre>

<p>The category <code>"numeric"</code> is a little tricky.
Although Portuguese and other Latin languages
use a comma instead of a point to represent decimal numbers,
the locale does not change the way that Lua parses numbers
(among other reasons because expressions like <code>print(3,4)</code>
already have a meaning in Lua).
Therefore, you may end with a system that cannot
recognize numbers with commas,
but cannot understand numbers with points either:
<pre>
    -- set locale for Portuguese-Brazil
    print(os.setlocale('pt_BR'))    --> pt_BR
    print(3,4)                      --> 3    4
    print(3.4)       --> stdin:1: malformed number near `3.4'
</pre>

<p>
<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="23.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

